package com.fgbf.todo.Activities;



import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.fgbf.todo.R;
import com.fgbf.todo.MainClasses.User;
import com.fgbf.todo.Managers.LoginManager;
import com.fgbf.todo.Managers.NotificationManager;

/**
 * GUI Class for the Create A User Screen
 * @author Warren Shenk
 * @date 2/20/2012
 */
public class NewUserActivity extends Activity {
	
	
	///////////////////////////////////////////////////////////////////////////////////
	// INSTANCE DATA
	///////////////////////////////////////////////////////////////////////////////////
	
	/**
	 * EditTexts
	 */
	private EditText userEtxt, pass1Etxt, pass2Etxt;
	/**
	 * Buttons
	 */
	private Button createButton;
	/**
	 * Instance of Login Manager to handle login situations
	 */
	private LoginManager lgMgr;
	
	
	///////////////////////////////////////////////////////////////////////////////////
	// METHODS
	///////////////////////////////////////////////////////////////////////////////////
	
	/** 
     * Called when the activity is first created. 
     */
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.newuser_screen);
		
		lgMgr = new LoginManager(NewUserActivity.this);
		
		userEtxt = (EditText) findViewById(R.id.etxt_username_attempt);
		pass1Etxt = (EditText) findViewById(R.id.etxt_password_attempt1);
		pass2Etxt = (EditText) findViewById(R.id.etxt_password_attempt2);
		
		createButton = (Button) findViewById(R.id.bttn_createuser);
		createButton.setOnClickListener(createListener);
	
	 }
	 //---------------------------------------------------------------------------------
	
	/**
	 * Called when back button is pressed 
	 * Kills the instance of NewUserActivity
	 */
	public void onBackPressed() {
		this.finish();
		startActivity(new Intent(this.getApplicationContext(), LoginActivity.class));
	}
	//---------------------------------------------------------------------------------

	
	
	///////////////////////////////////////////////////////////////////////////////////
	// LISTENERS
	///////////////////////////////////////////////////////////////////////////////////
	
	/**
	 * OnClickListener that responds to the createButton
	 */
	 private OnClickListener createListener = new OnClickListener() {
	    	public void onClick (View v) {
	    		
	    		String usrStr = userEtxt.getText().toString();
	    		String pass1Str = pass1Etxt.getText().toString();
	    		String pass2Str = pass2Etxt.getText().toString();
	    		
	    		boolean added = false;
	    		
	    		if (!lgMgr.userNameExists(usrStr) ){ 
	    			if (pass1Str.equals(pass2Str)) {
	    				lgMgr.addUserToDB(new User(usrStr, pass1Str));
	    				added = true;
	    			}
	    			else {
	    				//Notify user that passwords don't match
	    				NotificationManager.alertUserOK(NewUserActivity.this, "Passwords Do Not Match");
	    			}
	    		}
	    		else {
	    			//Notify user that username already exists
	    			NotificationManager.alertUserOK(NewUserActivity.this, "User Name Already Exists");
	    		}
	    		
	    		lgMgr.dumpUserDB();
	    		
	    		if (added)
	    			// Ends the current instance of NewUserActivity, starts login activity
	    			startActivity(new Intent(v.getContext(), LoginActivity.class));
	    			NewUserActivity.this.finish();  
	    		
	    			
	    	}
	 };
	 //---------------------------------------------------------------------------------

}
